<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="名称" clearable  class="w165"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="add" v-if="permission.add">增加</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.course.book.list" remoteSort>
                    <el-table-column label="名称" prop="name" align="left"></el-table-column>
                    <el-table-column label="课程数量" prop="courseNumber" width="150" align="center"></el-table-column>
                    <el-table-column label="排序" prop="sort" width="200" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="150">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_edit(scope.row, scope.$index)" v-if="permission.edit">编辑</el-button>
                            <el-divider direction="vertical" v-if="permission.delete"></el-divider>
                            <el-popconfirm :title="'确认是否要删除教材：' + scope.row.name + '?'" @confirm="table_del(scope.row)" v-if="permission.delete">
                                <template #reference>
                                    <el-button type="text" size="small" v-if="permission.delete">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" :parentId="parentId" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import { rolePermission } from '@/utils/permission'
import saveDialog from './save'
export default {
    components: {
        saveDialog
    },
    data() {
        return {
            dialog: {
                save: false
            },
            search: {
                name: null,
            },
            permission: {
				add: rolePermission('course-book-add'),
				edit: rolePermission('course-book-edit'),
				delete: rolePermission('course-book-delete'),
			}
        }
    },
    mounted() {
    },
    methods: {
        //添加
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑dee 
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //删除
        async table_del(row) {
            await this.$API.course.book.action.delete({ id: row.id }).then(() => {
                this.$refs.table.refresh()
                this.$notify.success("删除成功")
            });
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
.position {
    display: flex;
    align-items: center;
}
.position-item {
    margin-right: 5px;
}
.position-item:last-child {
    margin-right: 0;
}
</style>
